/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import java.util.List;

import com.elf.gwt.common.client.model.CategoryModel;
import com.elf.gwt.store.client.StoreComponentController;
import com.elf.gwt.store.client.utils.StoreUtils;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

/**
 *  
 */
public class CategoriesSummaryComponent extends StoreSimpleComponent
{	
	protected Grid bodyPanel;
	protected int maxNumCols = 5;
	protected int numCols = 0;

	/**
	 * Constructor
	 */
	public CategoriesSummaryComponent(StoreComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix, "dummy", false);
	}
	
	public boolean updateFromHistoryParameters(String[] params)
	{
		int index = 0;
		if (params.length > 1) index = Integer.parseInt(params[1]);
		CategoryModel model = StoreComponentController.categoriesComponent.getCategoryModel(index);
		StoreComponentController.categoriesComponent.getCategoryModel().update(model);
		StoreComponentController.productComponent.getProductModel().update(0, 0, "");
		storeController.getMultipleResultsComponent().addComponent(this);
		
		return super.updateFromHistoryParameters(params);
	}

	public void build()
	{
		CategoryModel categoryModel = StoreComponentController.categoriesComponent.getCategoryModel();
		boolean hideEmptyCategories = categoryModel.isHideEmptyCategories();
		this.maxNumCols = categoryModel.getCategoriesPerRowMax();
		this.getLeftTitleLabel().setText(categoryModel.getName());

		Image titleImage = this.getRightTitleImage();
		
		String imageName = categoryModel.getImage();
		if (imageName == null || imageName.length() == 0) imageName = "defaultCategoryImage.jpg";
		titleImage.setUrl(StoreUtils.getImageProductsDirectory() + imageName);
		String[] titleImageSize = categoryModel.getImageSize();
		titleImage.setSize(titleImageSize[0], titleImageSize[1]);
		titleImage.setTitle(categoryModel.getName());

		if (categoryModel.getChildList().size() == 0)
		{
			panel.setText(1, 0, "");
			onBuilt();
		}
		else 
		{
			int sz = categoryModel.getChildList().size();
			int szChildList = sz;
			List<CategoryModel> childList = categoryModel.getChildList();
			if (hideEmptyCategories)
			{
				sz = 0;
				for (int i = 0; i < szChildList; i++)
				{
					CategoryModel model = childList.get(i);
					if (model.getCount() > 0) sz++;
				}
			}

			numCols = Math.min(maxNumCols, sz);
			int numRows = (int)Math.round(Math.ceil((1.0 * sz)/(numCols * 1.0)));
			int colWidth = (int)Math.round(100.00 / numCols);

			bodyPanel = new Grid(numRows, numCols);
			bodyPanel.addStyleName("ComponentBodyPanel");
			bodyPanel.addStyleName(getHandle() + "BodyPanel");
			bodyPanel.addClickHandler(tableHandler); 
			CellFormatter cf = bodyPanel.getCellFormatter();
			int j = 0;
			for (int i = 0; i < szChildList; i++)
			{
				int row = (int)Math.round(Math.floor((1.0 * j)/(numCols * 1.0)));
				int col = j - (row * numCols);

				CategoryModel model = childList.get(i);
				if (!hideEmptyCategories || model.getCount() > 0) 
				{
					VerticalPanel vpanel = new VerticalPanel();
					vpanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
					Image image = new Image();
					image.setUrl(StoreUtils.getImageProductsDirectory() + model.getImage());
					image.setTitle(model.getName());
					String[] imageSize = model.getImageSize();
					image.setSize(imageSize[0], imageSize[1]);
					vpanel.add(image);
					vpanel.add(new InlineLabel(model.getName()));

					bodyPanel.setWidget(row, col, vpanel);
					cf.addStyleName(row, col, "ComponentLink");
					cf.addStyleName(row, col, "ComponentImage");
					cf.addStyleName(row, col, getHandle() + "Image");
					cf.setWidth(row, col, colWidth + "%");
					cf.setHorizontalAlignment(row, col, HasHorizontalAlignment.ALIGN_CENTER);
					j++;
				}
			}

			panel.setWidget(1, 0, bodyPanel);

			onBuilt();
		}
	}

	protected ClickHandler tableHandler = new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			HTMLTable.Cell cell = bodyPanel.getCellForEvent(event);
			int rowIndex = cell.getRowIndex();
			int cellIndex = cell.getCellIndex();

			CategoryModel categoryModel = StoreComponentController.categoriesComponent.getCategoryModel();
			CategoryModel model = categoryModel.getChildList().get((rowIndex * numCols) + cellIndex);
			
			if (model.getChildList().size() == 0 && model.getCount() > 0)
			{
				onSelection("AdvancedSearchResultsComponent--" + model.getId() 
						+ "--" + StoreComponentController.manufacturersComponent.getManufacturerModel().getId() + "--false--false--false--0--0--*--*--0--*--*");
			}
			else
			{
				onSelection("CategoriesSummaryComponent--" + model.getId());
			}
		}
	};
}
